<%@ page import="java.text.*,java.io.File" %>
<% 
  boolean allowabsolute=false; 
  boolean allowrecursivedelete=true; 
%>
<%! 
    private boolean recurseDelete(File folder)
    {
      boolean del = false;
      if (folder != null)
      {
        File[] files = folder.listFiles();
        if ((files != null) && (files.length>0))
        {
          for (int i=0;i<files.length;i++)
          {
            File file = files[i];
            if (file.isDirectory()) del = recurseDelete(file);
            else del = file.delete();
          }	
        }
        // Delete empty folder.
        del = folder.delete();
      }
      return del;
    }
%>
<%
  if (method.equalsIgnoreCase("post"))
  {
    String filesystem = request.getHeader("filesystem");
    if (filesystem != null)
    {
       String cmdStr = request.getParameter("cmd");
       if ((cmdStr != null) && (cmdStr.length()>0))
       {
          // Check input parameters.
          String val1 = request.getParameter("val1");
          if ((val1 != null) && (val1.length()>0))
          {
             if (allowabsolute == false)
             {
                if ((val1.indexOf("/..")>=0) || (val1.indexOf("\\..")>=0))
                {
                   response.sendError(HttpServletResponse.SC_FORBIDDEN);
                   return;
                }
                val1 = directory + val1;
             }
          }
          String val2 = request.getParameter("val2");
          if ((val2 != null) && (val2.length()>0))
          {
             if (allowabsolute == false)
             {
                if ((val2.indexOf("/..")>=0) || (val2.indexOf("\\..")>=0))
                {
                   response.sendError(HttpServletResponse.SC_FORBIDDEN);
                   return;                
                }
                val2 = directory + val2;
             }
          }
	      // Process filesystem command.
          if (cmdStr.equalsIgnoreCase("list"))
          {
             String dirStr = val1;
             if ((dirStr != null) && (dirStr.length()>0))
             {
                out.println("<pre>");
                File dir = new File(dirStr);
                File[] files = dir.listFiles();
                if (files != null)
                {
                   for (int i=0;i<files.length;i++)
                   {
                      File file = files[i];
                      String date = String.valueOf(file.lastModified());
                      String name = file.getName();
                      String size = String.valueOf(file.length());
                      String type = "file";
                      if (file.isDirectory()) type = "dir";
                      out.println(type + " " + date + " " + size + " " + name + "<br>");
                   }             
                }
                out.println("</pre>");
                return;
             }               
          }
          else if (cmdStr.equalsIgnoreCase("mkdir"))
          {
             String mkdirStr = val1;
             if ((mkdirStr != null) && (mkdirStr.length()>0))
             {
               File dir = new File(mkdirStr);
               boolean created = dir.mkdir();
               if (created == true) response.setStatus(HttpServletResponse.SC_NO_CONTENT);
               else response.sendError(HttpServletResponse.SC_FORBIDDEN);
               return; 
             }          
          }
          else if (cmdStr.equalsIgnoreCase("del"))
          {
             String delStr = val1;
             String recurseStr = request.getHeader("recursive");
             boolean recurse = false;
             if ((allowrecursivedelete==true) && (recurseStr != null) && (recurseStr.equalsIgnoreCase("true"))) recurse = true;
             if ((delStr != null) && (delStr.length()>0))
             {
               boolean deleted = false;
               File file = new File(delStr);
               if ((file.isDirectory()) && (recurse==true)) deleted = recurseDelete(file);
               else deleted = file.delete();
               if (deleted == true) response.setStatus(HttpServletResponse.SC_NO_CONTENT);
               else response.sendError(HttpServletResponse.SC_FORBIDDEN);
               return; 
             }          
          }                 
          else if (cmdStr.equalsIgnoreCase("move"))
          {
             String fromStr = val1;
             String toStr = val2;
             if ((fromStr != null) && (fromStr.length()>0) && (toStr != null) && (toStr.length()>0))
             {
               File from = new File(fromStr);
               File to = new File(toStr);
               boolean renamed = from.renameTo(to);
               if (renamed == true) response.setStatus(HttpServletResponse.SC_NO_CONTENT);
               else response.sendError(HttpServletResponse.SC_FORBIDDEN);
               return; 
             }          
          }
          else if (cmdStr.equalsIgnoreCase("download"))
          {
             if (keepalive == false) response.setHeader("Connection","close");
             String fileStr = val1;
             if ((fileStr != null) && (fileStr.length()>0))
             {
            	 File file = new File(fileStr);
            	 if (file.canRead())
            	 {
                     long skipBytes = 0;
                     // Resume support.
                     String range = request.getHeader("Range");                     
                     if (range != null)
                     {
                       range = range.substring(6,range.length());
                       range = range.substring(0,range.indexOf("-"));
                       skipBytes = Long.parseLong(range);
                     }
                	 response.setContentLength((int)(file.length()-skipBytes));
            		 FileInputStream fin = new FileInputStream(file);
            		 if (skipBytes > 0) fin.skip(skipBytes);
                	 OutputStream outstream = response.getOutputStream();
                	 byte[] buffer = new byte[4096];
                	 int read=-1;
                	 while ((read=fin.read(buffer)) != -1)
                	 {
                		 outstream.write(buffer, 0, read);
                	 }
                	 outstream.close();
                	 fin.close();
            	 }
            	 else response.sendError(HttpServletResponse.SC_FORBIDDEN);
             }
          }    
       }       
       return;
    }
  }
%>
